<?php
defined('IN_TS') or die('Access Denied.');

$userid = intval($TS_USER['user']['userid']);
 
switch($ts){

	//普通上传
	case "photo_add":
		
		if($userid=='0') qiMsg("非法操作！");
		
		//包含模版
		include TmooTemplate("ajax_photo_add");
		
		break;
		
	//Flash上传
	case "flash_add":
		if($userid=='0') qiMsg("非法操作！");
		include TmooTemplate("ajax_flash_add");
		break;
	
	
	case "photo_add_do":
		if($userid=='0') qiMsg("非法操作！");
		$uptypes = array( 
			'image/jpg',
			'image/jpeg',
			'image/png',
			'image/pjpeg',
			'image/gif',
			'image/bmp',
			'image/x-png',
		);
	
		if($_FILES['photo']['name'][0] == '') qiMsg("上传图片不能为空！");
		
		//处理目录存储方式
		$menu = substr($userid,0,1);
		
		$date = date('Ymd');
		
		$dest_dir='data/uploadfile/photo/'.$menu.'/'.$date.'/'.$userid;
		createFolders($dest_dir);
		
		if(isset($_FILES['photo'])){
			$arrFileName = $_FILES['photo']['name'];
			
			foreach($arrFileName as $key=>$item){
				if($item != ''){
					$phototype = $_FILES['photo']['type'][$key];
					$photosize = $_FILES['photo']['size'][$key];
					if (!in_array($phototype,$uptypes)) {
						qiMsg("只支持图片上传！");
					}
					if($photosize>1024000){
						qiMsg("最大只支持1M的图片！");
					}
				}
			}
			
			foreach($arrFileName as $key=>$item){
				if($item != ''){
				
					$photoname = $item;
					$phototype = $_FILES['photo']['type'][$key];
					$photosize = $_FILES['photo']['size'][$key];
					
					$fileInfo=pathinfo($item);
					$extension=$fileInfo['extension'];
					
					//$dest=$dest_dir.'/'.$item;
					$dest=$dest_dir.'/'.date("YmdHis").mt_rand(10000,99999).'.'.$extension;
					move_uploaded_file($_FILES['photo']['tmp_name'][$key], iconv("UTF-8","gb2312",$dest));
					chmod($dest, 0755);
					
					$arrData = array(
						'userid'	=> $userid,
						'photoname'	=> $photoname,
						'phototype'	=> $phototype,
						'photourl'		=> $dest,
						'photosize'		=> $photosize,
						'addtime'	=> time(),
					);
					
					$attachid = $DB->insertArr($arrData,'app_photo');
					
				}
				
			}
		}
		
		header("Location: index.php?app=photo&ac=ajax&ts=photo_my");
	
		break;
		
	//通过flash上传的接口 
	case "flash_add_do":
		
		$userid = intval($_GET['userid']);
		if($userid=='0') qiMsg("非法操作！");
		if (!empty($_FILES)) {
	
			$menu = substr($userid,0,1);
			$date = date('Ymd');
			$dest_dir='data/uploadfile/photo/'.$menu.'/'.$date.'/'.$userid;
			createFolders($dest_dir);
			
			$photoname = $_FILES["Filedata"]['name'];
			$phototype = $_FILES['Filedata']['type'];
			$photosize = $_FILES['Filedata']['size'];
			
			$fileInfo=pathinfo($photoname);
			$extension=$fileInfo['extension'];

			$dest=$dest_dir.'/'.date("YmdHis").mt_rand(10000,99999).'.'.$extension;
			move_uploaded_file($_FILES['Filedata']['tmp_name'], iconv("UTF-8","gb2312",$dest));
			chmod($dest, 0755);
			
			$arrData = array(
				'userid'	=> $userid,
				'photoname'	=> $photoname,
				'phototype'	=> $phototype,
				'photourl'		=> $dest,
				'photosize'		=> $photosize,
				'addtime'	=> time(),
			);
			
			$attachid = $DB->insertArr($arrData,'app_photo');
			
			echo $attachid;
		
		}
		
		break;
		
	case "photo_my":
		if($userid=='0') qiMsg("非法操作！");
		$arrPhoto = $DB->fetch_all_assoc("select * from ".dbprefix."app_photo where userid='$userid'");
		
		include TmooTemplate("ajax_photo_my");
		
		break;
}